package linwen.wang.redis.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;

import java.util.List;

/**
 * @ClassName JedisTran
 * @Description
 * @Author lin
 * @Date 2022/3/2 下午9:42
 * @Version V1.0
 */
public class JedisTran {
    @Test
    public void test(){
        Jedis jedis = new Jedis();
        System.out.println(jedis.ping());
        Transaction multi = jedis.multi();
        try{
            multi.set("user:name","zhangsan");
            multi.set("user:age","18");
            multi.exec();
        }catch (Exception e){
            System.out.println(e);
            multi.discard();
        }finally {
            System.out.println(jedis.get("user:name"));
            System.out.println(jedis.get("user:age"));
            jedis.close();
        }
    }

    @Test
    public void test1() throws InterruptedException {
        Jedis jedis = new Jedis();
        jedis.set("user:name","zhangsan");
        jedis.watch("user:name");
        Transaction multi = jedis.multi();
        multi.set("user:name","zhangsan111");
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Jedis jedis1 = new Jedis();
                jedis1.set("user:name", "lisi");
                System.out.println(jedis1.get("user:name")+"11111111");
            }
        });
        thread.start();
        Response<String> stringResponse = multi.get("user:name");
        System.out.println(stringResponse+"000000");
        Thread.sleep(3000);
        List<Object> exec = multi.exec();
        System.out.println(exec+"-----");
    }
}
